package cn.feuler.code8081.controller;

import cn.feuler.code8081.client.EmailClient;
import cn.feuler.code8081.service.AuthCodeService;
import cn.feuler.common.entity.AuthCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/code")
public class CodeController {
    private final AuthCodeService authCodeService;

    private final EmailClient emailClient;

    public CodeController(AuthCodeService authCodeService, EmailClient emailClient) {
        this.authCodeService = authCodeService;
        this.emailClient = emailClient;
    }

    @GetMapping("create/{email}")
    public boolean create(@PathVariable String email) {
        AuthCode code = authCodeService.create(email);
        System.out.println("email: [" + email + "] code: [" + code + "]");
        return emailClient.SendEmail(email, code.getCode());
    }

    @GetMapping("validate/{email}/{code}")
    public Integer validate(@PathVariable String email, @PathVariable String code) {
        return authCodeService.validate(email, code);
    }
}
